------------------------------------------------------------------------------------------------------------------------------------------------
local function language_change_say(theNPC)
    theNPC.CHAT_TABLE = {}
    pcall(function()
        -- theNPC.npc_base_lib:Get_Language() == "ch"
        theNPC.CHAT_TABLE = TUNING.NPC_CHAT_TABLE[ theNPC.npc_base_lib:Get_Language() ] ["npc_event_trident"] or {}
    end)
end
------------------------------------------------------------------------------------------------------------------------------------------------
local function Time_Out_Leave(inst)
    local theNPC = inst.entity:GetParent()
    local time = theNPC.npc_base_lib:Get_Time_Out()
    if time then
        inst:DoTaskInTime(time,function()
            inst:Remove()
            theNPC:AddDebuff("npc_event_leave","npc_event_leave")
            -- theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.time_out)
            print("Warning : npc time out and leave")
        end)
    end
end
local function Fall_down_fish(Point)
    -- print("Fall_down_fish : ",Point.x,Point.y,Point.z)
    local fish_list = {}
    for i = 1, 9, 1 do
        table.insert(fish_list,"oceanfish_small_"..i)
        table.insert(fish_list,"oceanfish_medium_"..i)
    end
    table.insert(fish_list,"wobster_sheller")
    table.insert(fish_list,"wobster_sheller")
    table.insert(fish_list,"wobster_moonglass")
    -- table.insert(fish_list,"wobster_sheller")


    local temp = fish_list[math.random(#fish_list)]
    local theFish = SpawnPrefab(temp)
    

    local theFish2 = theFish.components.oceanfishable:MakeProjectile()
    -- theFish2.Transform:SetPosition(Point.x,0.1,Point.z)
    theFish2.Transform:SetPosition(Point.x,Point.y,Point.z)

    local ae_cp = theFish2.components.complexprojectile
    if ae_cp then
        ae_cp:SetHorizontalSpeed(0)
        ae_cp:SetGravity(-30)
        ae_cp:SetLaunchOffset(Vector3(0, .1, 0))
        ae_cp:SetTargetOffset(Vector3(0, .1, 0))

        local v_position = theFish:GetPosition()
        local launch_position = v_position + (v_position - Point):Normalize() * 8
        ae_cp:Launch(launch_position, theFish2)
    end

    -- theFish2:DoTaskInTime(0.2,function()
    --     theFish2.Transform:SetPosition(Point.x,Point.y,Point.z)
    -- end)

end

-- local function Fall_down_fish2(pt,name)
--     local fish_list = {}
--     for i = 1, 9, 1 do
--         table.insert(fish_list,"oceanfish_small_"..i.."_inv")
--         table.insert(fish_list,"oceanfish_medium_"..i.."_inv")
--     end
--     local ret_name = fish_list[math.random(#fish_list)]
-- end
local function trident_task(theNPC,theplayer)
    -- local mid_pt = ( Vector3(theNPC.Transform:GetWorldPosition()) + Vector3( theplayer.Transform:GetWorldPosition() )  ) / 2

    local theNPC_PT = Vector3(theNPC.Transform:GetWorldPosition() )
    local theplayer_PT = Vector3( theplayer.Transform:GetWorldPosition() )
    local mid_pt = Vector3(  (theNPC_PT.x + theplayer_PT.x)/2 , 0 , (theNPC_PT.z + theplayer_PT.z)/2   )


    local fishs_num = 40
    if TUNING.NPC_CONFIG.REWARDS_LESS == true then
        fishs_num = 25
    end


    local fish_name_list = {}
    for i = 1, 9, 1 do
        table.insert(fish_name_list,"oceanfish_small_"..i.."_inv")
        table.insert(fish_name_list,"oceanfish_medium_"..i.."_inv")
    end


    for i = 1, fishs_num, 1 do
        theNPC:DoTaskInTime(0.3*i,function()
            local pt = theNPC.npc_base_lib:GetSpawnPoint(mid_pt,math.random(80)/10) or mid_pt
            SpawnPrefab("npc_fx_waterspout"):PushEvent("Set",{pt = pt})
            pt.y = math.random(15,30)
            -- Fall_down_fish(pt)

            local ret_fish_name = fish_name_list[math.random(#fish_name_list)]  
            local ret_inst = SpawnPrefab(ret_fish_name) or SpawnPrefab("driftwood_log")
            ret_inst.Transform:SetPosition(pt.x,pt.y,pt.z)

        end)
    end


end


local function Shark_task(theNPC,theplayer)
    -- local mid_pt = ( Vector3(theNPC.Transform:GetWorldPosition()) + Vector3( theplayer.Transform:GetWorldPosition() )  ) / 2
    local theNPC_PT = Vector3(theNPC.Transform:GetWorldPosition() )
    local theplayer_PT = Vector3( theplayer.Transform:GetWorldPosition() )
    local mid_pt = Vector3(  (theNPC_PT.x + theplayer_PT.x)/2 , 0 , (theNPC_PT.z + theplayer_PT.z)/2   )

    -- local pt = theNPC.npc_base_lib:GetSpawnPoint(mid_pt,math.random(60)/10) or mid_pt
    local pt = mid_pt

    local theShark = SpawnPrefab("shark")
    theShark.Transform:SetPosition(pt.x,0,pt.z)
    SpawnPrefab("npc_fx_waterspout"):PushEvent("Set", { pt = pt,scale = Vector3(3,3,3) })
    theShark:SetBrain(nil)

    theShark.components.combat:SetDefaultDamage(100000)
    theShark.__brain_event = function(theShark)
        -- print("theShark attacked")
        theShark:SetBrain(require("brains/sharkbrain"))
        theShark:RestartBrain()
        theShark:RemoveEventCallback("attacked",theShark.__brain_event)
        theShark:DoTaskInTime(60,function()
            theShark.components.combat:SetDefaultDamage(TUNING.SHARK.DAMAGE)            
        end)
        if theShark.___death_task then
            theShark.___death_task:Cancel()
        end
    end
    theShark:ListenForEvent("attacked",theShark.__brain_event)

    theShark.___death_task = theShark:DoPeriodicTask(1,function()
        local health_delta = theShark.components.health.maxhealth / 60
        theShark.components.health:DoDelta(-health_delta)
    end)

end

-------------------------------------------------------------------------------------------------------------------------------------------------




local function NPC_TASKS_INIT(inst)
    local theNPC = inst.entity:GetParent()
    language_change_say(theNPC)
    Time_Out_Leave(inst)
    
    theNPC.npc_base_lib:Equip_Random_Hat()
    theNPC.npc_base_lib:Equip_Random_Cloth()
    -- theNPC.npc_base_lib:Equip_Random_Weapon()
    theNPC.npc_base_lib:Equip(SpawnPrefab("trident"))
    theNPC:AddTag("INLIMBO")
    theNPC:AddTag("notarget")
    theNPC:AddTag("noattack")
    theNPC:AddTag("invisible")
    -- theNPC:AddTag("Unable_To_Refuse")
    theNPC.npc_event = inst.prefab
end

local function NPC_TASKS(inst)
    local theNPC = inst.entity:GetParent()
    if TheWorld.state.isnight  or TheWorld:HasTag("cave") then
        theNPC.npc_base_lib:Try_2_Set_Light(true)
    else
        theNPC.npc_base_lib:Try_2_Set_Light(false)
    end
    local theplayer = theNPC.__link_player
    if theplayer == nil or theplayer:IsValid() == false then
        -- theNPC:RemoveDebuff(inst.prefab)
        inst:Remove()
        theNPC:AddDebuff("npc_event_leave","npc_event_leave")
        print("Warning : theNPC.__link_player == nil")
        return
    end

    if theNPC.event_step == nil then

        if theNPC.npc_base_lib:Close_2_Target_In_Range({range = 6}) == true then
            theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.found_trident)
            theNPC.event_step = 1
        end        
    elseif theNPC.event_step == 1 then
        if theNPC.npc_everything_data:Add("task_time_wait_00",1)%4 == 0 then
            theNPC.event_step = theNPC.event_step + 1
        end
    elseif theNPC.event_step == 2 then

        theNPC.npc_base_lib:Action_Gita()
        theNPC.event_step = theNPC.event_step + 1
        theNPC:DoTaskInTime(1.4,function()
            trident_task(theNPC,theplayer)
        end)
    elseif theNPC.event_step == 3 then
       if theNPC.npc_everything_data:Add("task_time_wait_01",1)%20 == 0 then
            theNPC.event_step = theNPC.event_step + 1
        end        
    elseif theNPC.event_step == 4 then
        theNPC.npc_base_lib:Action_Gita()
        theNPC.event_step = theNPC.event_step + 1       
        theNPC:DoTaskInTime(1.2,function()
            -- freeze_players(theplayer)
            Shark_task(theNPC,theplayer)
        end)
    
    elseif theNPC.event_step == 5 then
        if theNPC.npc_everything_data:Add("task_time_wait_02",1)%10 == 0 then
            theNPC.event_step = theNPC.event_step + 1
        end
    elseif theNPC.event_step == 6 then
        -- theNPC.event_step = theNPC.event_step + 1
        -- theNPC:RemoveDebuff(inst.prefab)
        inst:Remove()
        theNPC.npc_base_lib:Say("Bye ~ ")
        -- theNPC.npc_base_lib:Say(theNPC.CHAT_TABLE.hahaha)

        theNPC:AddDebuff("npc_event_leave","npc_event_leave")
    end
end
--------------------------------------------------------------------------------------------------------------------------------------------------

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    local NPC = inst.entity:GetParent()
    local flag ,error_code= pcall(function(inst,target)
        if TheWorld.ismastersim then

            NPC_TASKS_INIT(inst)


        end
    end,inst,target)
    if flag == false  then
        print("Error : OnAttached(inst,target) ",inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    local NPC = inst.entity:GetParent()
    local flag,error_code = pcall(function(inst,ismastersim)
        -- local NPC = inst.entity:GetParent()

        NPC_TASKS(inst)


    end,inst,ismastersim)
    if flag == false then
        print("Error : OnUpdate(inst,ismastersim) ,step:",NPC.event_step ,inst.prefab)
        print(error_code)
        NPC:Remove()
    end
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 0.5, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_trident", fn)
